<?php

namespace App\Http\Controllers\Auth;

use App\Helpers\CustomerValidator;
use App\Http\Controllers\Controller;
use App\Models\Code;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class LoginController extends Controller
{
    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'platform' => 'required',
        ], [
            'platform.required' => '设备不能为空',
        ]);

        if ($validator->fails()) {
            return jsonResponse(config('common.validatorError'), $validator);
        }
        $platform = $request->get('platform');
        if (in_array($platform, config('common.platform.auth2'))) {
            // 第三方认证
            # TODO
            // 微信登录部分
            return '功能暂未开放';
        } else {
            // 1：密码登录，2：手机验证码登录
            $type = $request->get('type', 1);
            $token = hash('sha256', Str::random(60));
            $agent = $request->header('user-agent');
            switch ($type) {
                case 1: {
                    $name = $request->get('name');
                    if (empty($name)) {
                        return jsonResponse(41001);
                    }
                    $password = $request->get('password');
                    if (empty($password)) {
                        return jsonResponse(41004);
                    }
                    $user = User::where('name', $name)
                        ->orWhere('email', $name)
                        ->orWhere('phone', $name)
                        ->first();
                    if (empty($user)) {
                        return jsonResponse(41000);
                    } else {
                        $status = Hash::check($password, $user['password']);
                        if ($status) {
                            $user['token'] = $token;
                            $user['last_ip'] = $request->getClientIp();
                            $user['last_time'] = time();
                            $user->save();
                            setLoginLog($user, 3, $agent);
                            return jsonResponse(config('common.successCode'), $user);
                        } else {
                            return jsonResponse(41002);
                        }
                    }
                }
                case 2: {
                    $mobile = $request->get('mobile');
                    $valid = new CustomerValidator();
                    if ($valid->checkMobile($mobile)) {
                        $number = $request->get('code');
                        $code = Code::where('device', $mobile)->where('code', $number)->first();
                        if (empty($code)) {
                            return jsonResponse(41007);
                        } else {
                            // 这里不判断用户是否存在，在发送手机验证码的时候验证手机是否存在，不存在就创建
                            $user = User::where('phone', $mobile)->first();
                            $user['token'] = $token;
                            $user['last_ip'] = $request->getClientIp();
                            $user['last_time'] = time();
                            $user->save();
                            setLoginLog($user, 3, $agent);
                            return jsonResponse(config('common.successCode'), $user);
                        }
                    } else {
                        return jsonResponse(41003);
                    }
                }
                default: {
                    return jsonResponse(40001);
                }
            }
        }
    }

    public function loginFailure()
    {
        return jsonResponse(41008);
    }

    public function noPrivilege()
    {
        return jsonResponse(41009);
    }
}
